package org.jadapter.examples.foobar;

import static org.junit.Assert.assertTrue;

import org.jadapter.Adapter;
import org.jadapter.JAdapter;
import org.jadapter.registry.TransformerRegistry;
import org.jadapter.registry.TransitiveAdapterRegistry;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestFooBar {

    @Test public void shouldWorkUsingTransformer() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext
            ("org/jadapter/examples/foobar/adapters.xml");
        
        FooBean foo = new FooBean();
        foo.setFoo(42);
                    
        UsesTransformer ub = (UsesTransformer)ctx.getBean("usesTransformer");
        ub.doStuffUsingTransformer(foo);
        

    }
    
    @Test public void shoudlWorkUsingRegistry() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext
            ("org/jadapter/examples/foobar/adapters.xml");
        
        FooBean foo = new FooBean();
        foo.setFoo(42);
        
        UsesRegistry ur = (UsesRegistry)ctx.getBean("usesRegistry");
        ur.doStuffUsingTransformer(foo);
        

    }
    
    @Test public void shouldUseConventionOverConfiguration () {
    	Adapter<Bar,Foo> fooBar = new JAdapter<Bar, Foo>(FooBar.class);
    	
    	Foo foo = new FooBean(42);
    	Bar bar = fooBar.transform(foo);
    	assertTrue(bar.bar() > 42);
    	
    }
    
    @Test public void shouldUseCoCInRegistry() {
    	Adapter fooBar = new JAdapter(FooBar.class);
    	TransformerRegistry transformers = new TransitiveAdapterRegistry(fooBar);
    	
    	Foo foo = new FooBean(42);
    	Bar bar = transformers.transform(foo, Bar.class);
    	assertTrue(bar.bar() > 42);   	
    	
    }

}
